package com.openmanus.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * OpenManus 配置属性
 * 
 * @author OpenManus Team
 * @version 1.0.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "openmanus")
public class OpenManusProperties {

    /**
     * 智能体配置
     */
    private AgentProperties agent = new AgentProperties();

    /**
     * 工具配置
     */
    private ToolsProperties tools = new ToolsProperties();

    /**
     * 流程配置
     */
    private FlowProperties flow = new FlowProperties();

    /**
     * 记忆配置
     */
    private MemoryProperties memory = new MemoryProperties();

    /**
     * 安全配置
     */
    private SecurityProperties security = new SecurityProperties();

    /**
     * Docker 配置
     */
    private DockerProperties docker = new DockerProperties();

    @Data
    public static class AgentProperties {
        /**
         * 最大执行步数
         */
        private int maxSteps = 20;

        /**
         * 执行超时时间
         */
        private Duration timeout = Duration.ofMinutes(5);

        /**
         * 默认温度参数
         */
        private double defaultTemperature = 0.7;
    }

    @Data
    public static class ToolsProperties {
        /**
         * Python 工具配置
         */
        private PythonProperties python = new PythonProperties();

        /**
         * 浏览器工具配置
         */
        private BrowserProperties browser = new BrowserProperties();

        /**
         * 搜索工具配置
         */
        private SearchProperties search = new SearchProperties();

        @Data
        public static class PythonProperties {
            /**
             * 沙盒配置
             */
            private SandboxProperties sandbox = new SandboxProperties();

            @Data
            public static class SandboxProperties {
                /**
                 * 是否启用沙盒
                 */
                private boolean enabled = true;

                /**
                 * Docker 镜像
                 */
                private String image = "python:3.11-slim";

                /**
                 * 执行超时时间
                 */
                private Duration timeout = Duration.ofSeconds(30);

                /**
                 * 内存限制
                 */
                private String memoryLimit = "512m";

                /**
                 * 是否允许网络访问
                 */
                private boolean networkEnabled = false;
            }
        }

        @Data
        public static class BrowserProperties {
            /**
             * 是否启用浏览器工具
             */
            private boolean enabled = true;

            /**
             * 是否无头模式
             */
            private boolean headless = true;

            /**
             * 操作超时时间
             */
            private Duration timeout = Duration.ofSeconds(60);

            /**
             * 用户代理
             */
            private String userAgent = "OpenManus Browser Agent 1.0";
        }

        @Data
        public static class SearchProperties {
            /**
             * 是否启用搜索工具
             */
            private boolean enabled = true;

            /**
             * 默认搜索引擎
             */
            private String defaultEngine = "google";

            /**
             * 最大结果数量
             */
            private int maxResults = 10;

            /**
             * 搜索超时时间
             */
            private Duration timeout = Duration.ofSeconds(10);
        }
    }

    @Data
    public static class FlowProperties {
        /**
         * 最大并发智能体数量
         */
        private int maxConcurrentAgents = 5;

        /**
         * 任务超时时间
         */
        private Duration taskTimeout = Duration.ofMinutes(10);

        /**
         * 是否启用规划功能
         */
        private boolean enablePlanning = true;
    }

    @Data
    public static class MemoryProperties {
        /**
         * 记忆存储类型
         */
        private String type = "pgvector";

        /**
         * 最大记忆条目数
         */
        private int maxItems = 10000;

        /**
         * 清理间隔
         */
        private Duration cleanupInterval = Duration.ofHours(1);

        /**
         * 默认过期时间
         */
        private Duration defaultExpiry = Duration.ofDays(7);
    }

    /**
     * 数据持久化配置
     */
    private PersistenceProperties persistence = new PersistenceProperties();

    @Data
    public static class PersistenceProperties {
        /**
         * 数据库类型: postgresql, mysql, h2
         */
        private DatabaseType database = DatabaseType.POSTGRESQL;

        /**
         * ORM框架类型: jpa, mybatis_plus, mybatis
         */
        private OrmType orm = OrmType.JPA;

        /**
         * 是否启用向量存储（仅PostgreSQL支持）
         */
        private boolean enableVectorStore = true;

        /**
         * 是否启用分布式缓存
         */
        private boolean enableDistributedCache = true;

        /**
         * 数据库连接池配置
         */
        private PoolProperties pool = new PoolProperties();

        @Data
        public static class PoolProperties {
            /**
             * 最大连接数
             */
            private int maximumPoolSize = 20;

            /**
             * 最小空闲连接数
             */
            private int minimumIdle = 5;

            /**
             * 连接超时时间（毫秒）
             */
            private long connectionTimeout = 20000;

            /**
             * 空闲超时时间（毫秒）
             */
            private long idleTimeout = 300000;

            /**
             * 最大生命周期（毫秒）
             */
            private long maxLifetime = 1800000;
        }

        public enum DatabaseType {
            POSTGRESQL("postgresql", "org.postgresql.Driver", "org.hibernate.dialect.PostgreSQLDialect"),
            MYSQL("mysql", "com.mysql.cj.jdbc.Driver", "org.hibernate.dialect.MySQL8Dialect"),
            H2("h2", "org.h2.Driver", "org.hibernate.dialect.H2Dialect");

            private final String name;
            private final String driverClass;
            private final String hibernateDialect;

            DatabaseType(String name, String driverClass, String hibernateDialect) {
                this.name = name;
                this.driverClass = driverClass;
                this.hibernateDialect = hibernateDialect;
            }

            public String getName() { return name; }
            public String getDriverClass() { return driverClass; }
            public String getHibernateDialect() { return hibernateDialect; }
        }

        public enum OrmType {
            JPA("jpa", "Spring Data JPA + Hibernate"),
            MYBATIS_PLUS("mybatis-plus", "MyBatis-Plus"),
            MYBATIS("mybatis", "MyBatis");

            private final String name;
            private final String description;

            OrmType(String name, String description) {
                this.name = name;
                this.description = description;
            }

            public String getName() { return name; }
            public String getDescription() { return description; }
        }
    }

    @Data
    public static class SecurityProperties {
        /**
         * 是否启用认证
         */
        private boolean enableAuth = true;

        /**
         * JWT 密钥
         */
        private String jwtSecret = "openmanus-jwt-secret-key-change-in-production";

        /**
         * JWT 过期时间
         */
        private Duration jwtExpiration = Duration.ofHours(24);
    }

    @Data
    public static class DockerProperties {
        /**
         * 是否启用 Docker
         */
        private boolean enabled = true;

        /**
         * Docker 主机地址
         */
        private String host = "unix:///var/run/docker.sock";

        /**
         * Docker API 版本
         */
        private String apiVersion = "1.41";
    }
}
